package com.walker.taogong.dal.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.dal.UserDal;
import com.walker.taogong.datamodel.User;
import com.walker.taogong.datamodel.Worker;
import com.walker.taogong.hibernate.HibernateUtil;

/**
 * name dao' s heibernate impl
 * 
 * @author Alex Wei
 * 
 */
public class UserDalImpl extends BaseDal implements UserDal
{

    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(UserDalImpl.class);
    
    @Override
    public User getUserById(int id)
    {

        // String hql = " from Name where id = :nameId ";
        // Query query =
        // HibernateUtil.getCurrentSession().createQuery(hql).setLong("nameId",
        // nameId);
        // Name name = (Name)query.uniqueResult();
        User name =(User) HibernateUtil.getCurrentSession().get(User.class,id);
        HibernateUtil.commit();

        return name;

    }

    @Override
    public List<User> getUserList()
    {

        String hql = " from User ";
        Query query = HibernateUtil.getCurrentSession().createQuery(hql);
        @SuppressWarnings("unchecked")
        List<User> nameList = query.list();
        HibernateUtil.commit();

        return nameList;

    }

    @Override
    public void saveUser(User user)
    {
        HibernateUtil.getCurrentSession().saveOrUpdate(user);
        HibernateUtil.commit();

    }

    @Override
    public User getUserByUserName(String userName)
    {
        String hql = " from User u where u.userName = :name";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql)
                .setParameter("name", userName);
        
        return (User)query.uniqueResult();
    }

    @Override
    public User getUserByEmail(String email)
    {
        String hql = " from User u where u.email = :email";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql)
                .setParameter("email", email);
        
        return (User)query.uniqueResult();
    }

    @Override
    public User getUserByMobile(String mobile)
    {
        String hql = " from User u where u.mobile = :mobile";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql)
                .setParameter("mobile", mobile);
        
        return (User)query.uniqueResult();
    }

    @Override
    public User getUserByEmailAndPwd(String email, String password)
    {
        String hql = " from User u where u.email = :email and u.password = :pwd";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql)
                .setParameter("email", email)
                .setString("pwd", password);
        
        return (User)query.uniqueResult();
    }

    @Override
    public User getUserByMobileAndPwd(String mobile, String password)
    {
        String hql = " from User u where u.mobile = :mobile and u.password = :pwd";
        Query query = HibernateUtil.getCurrentSession()
                .createQuery(hql)
                .setParameter("mobile", mobile)
                .setString("pwd", password);
        
        return (User)query.uniqueResult();
    }
    
    @Override
    public void saveWorker(Worker worker)
    {
        HibernateUtil.getCurrentSession().saveOrUpdate(worker);
        HibernateUtil.commit();

    }
    
    
    /*************************启动組件**********************************************/
    /**
     * 组件启动初期化
     */
    @Override
    protected void activate(ComponentContext cc) 
    {
        log.info("{} is starting!", cc.getProperties().get("name") );
        super.activate(cc);
        
    }

}
